package cn.jasonone.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginFilter implements Filter {

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse resp = (HttpServletResponse) response;
		
		String uri = req.getRequestURI();
		uri = uri.substring(req.getContextPath().length());
		
		if(uri.startsWith("/login") || uri.equals("/index.html")) {
			chain.doFilter(request, response);
			return;
		}
		if(uri.endsWith(".css")) {
//			response.setContentType("text/css");
			chain.doFilter(request, response);
			return;
		}
		if(uri.endsWith(".js")) {
//			response.setContentType("application/javascript");
			chain.doFilter(request, response);
			return;
		}
		
		
		
		if(req.getSession().getAttribute("LOGIN_STATUS")!=null) {
			chain.doFilter(request, response);
		}else {
			resp.sendRedirect(req.getContextPath()+"/login.jsp");
		}
	}

}
